<template>
  <el-dialog
    :visible.sync="dialogModel"
    :title="title" @close="close"
    :close-on-click-modal="false"
    :close-on-press-escape="true"
    size="small">
    <div class="clearBoth wrap">
      <!--工单退回-->
      <business-back
        style="padding-top: 0px"
        v-if="type === 'back'"
        :business="{}"
        :showTitle="false"
        ref="back"
        @event="businessEvent"></business-back>
      <!--工单动作-->
      <approval
        v-if="type=== 'approval'"
        :business="{}"
        :showTitle="false"
        @event="businessEvent"
        ref="approval"
        :batch="true"
        :process="process"
      ></approval>
    </div>
  </el-dialog>
</template>
<script>
  // import { deepClone, removeEmptyProperty } from '@/utils/m7Utils'
  import businessBack from '@/components/public-modules/cust-tab-content/business/businessBack'
  import approval from '@/components/public-modules/cust-tab-content/business/Approval'
  export default {
    name: 'BatchExecute',
    data () {
      return {
        dialogModel: false,
        type: '',
        title: '',
        process: {}, // 工单下一步信息
        condition: {} // 批量操作条件
      }
    },
    props: {
    },
    computed: {
    },
    methods: {
      open ({type, busData, condition, title}) {
        this.dialogModel = true
        this.type = type
        this.title = title
        this.condition = condition
        if (type === 'approval') {
          this.process = busData
        }
        if (type === 'back' && this.$refs.back) {
          this.$refs.back.reset()
        }
      },
      // 提交工单事件
      businessEvent ({data, event}) {
        if (event === 'backAction') {
          this.saveBack(data.form)
        } else if (event === 'stepAction') {
          this.saveExecute(data.form)
        }
      },
      // 回退工单提交
      saveBack (form) {
        delete form._id
        this.condition.data = form
        this.$store.dispatch('batchBack', this.condition).then((res) => {
          if (res && res.success) {
            this.close()
            this.$emit('batchExecuteSuc', res,'back')
          }
        })
      },
      // 执行工单提交
      saveExecute (form) {
        delete form._id
        this.condition.data = form
        this.$store.dispatch('batchExecute', this.condition).then((res) => {
          if (res && res.success) {
            this.close()
            this.$emit('batchExecuteSuc', res)
          }
        })
      },
      close () {
        this.dialogModel = false
      }
    },
    components: {
      businessBack,
      approval
    }
  }
</script>
<style lang="stylus" scoped>
  .wrap
    padding 0 10px
</style>
